package zzu.mxd.answer.serviceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import zzu.mxd.answer.entity.Student;
import zzu.mxd.answer.mapper.RecordingMapper;
import zzu.mxd.answer.mapper.StudentMapper;
import zzu.mxd.answer.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import zzu.mxd.answer.vo.RecordExtend;
import zzu.mxd.utils.RedisUtils;

import java.util.List;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author mxd
 * @since 2018-11-08
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    RedisUtils redisUtils;

    @Override
    public void deleteFalsely(String id) {
        Student student = new Student();
        student.setStudentNumber(id);
        student.setStatus(0);//0删除1正常
        studentMapper.updateById(student);
    }
    /**
     *
     * @param id
     * @param password
     * @param verification
     * @param verifyKey
     * @return  int
     * 0 登陆成功
     * 1 验证码错误
     * 2 用户名不存在
     * 3 用户密码错误
     * 4 用户被冻结
     * 5 异常，登陆失败
     */
    @Override
    public int login(String id, String password, String verification,String verifyKey) {
        //验证码
        if (redisUtils.exists(verifyKey)){
            String verfiCode = redisUtils.get(verifyKey).toString();
            //不匹配大小写
            if (verfiCode.equalsIgnoreCase(verification)){
                //匹配密码
                Student student = studentMapper.selectById(id);
                if (student!=null){
                    if (student.getPassword().equals(password)){
                        if(student.getStatus()!=0){
                            return 0;
                        }else {
                            return 4;
                        }
                    }else {
                        return 3;
                    }
                }else {
                    return 2;
                }

            }else{
                return 1;
            }
        }else {
            log.error("数据库中获取不到验证码");
            return 5;
        }

    }


    @Override
    public boolean updatePassword(String id, String oldPassword, String newPassword, String renewPassword) {
        Student student = studentMapper.selectById(id);
        if (student.getPassword().equalsIgnoreCase(oldPassword)){
            if (newPassword.equals(renewPassword)){
                student.setPassword(newPassword);
                if (studentMapper.updateById(student)!=0)
                    return true;
                return false;
            }
            return false;
        }
        return false;
    }

    @Override
    public Student selectByIdCard(String idCard) {
        return studentMapper.selectByIdCard(idCard);
    }

    @Override
    public List<RecordExtend> getUserExercise(String id, Integer isRight) {



        return recordingMapper.selectRecordingExByStudentId(id,isRight);
    }
    @Autowired
    private RecordingMapper recordingMapper;
}
